package com.ruoyi.controller.jpjs.bi_deviationanalysis;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.jpjs.bi_deviationanalysis.DeviationAnalysisService;
import com.ruoyi.service.jpjs.bi_importantmatter.ImportantMatterService;
import com.ruoyi.service.jpjs.bi_operate.OperateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/deviationAnalysis")
public class DeviationAnalysisController extends BaseController {

    private String prefix = "jpjs/bi_deviationanalysis";

    @Autowired
    private ImportantMatterService importantMatterService;

    @Autowired
    private DeviationAnalysisService deviationAnalysisService;

    @GetMapping()
    public String list(ModelMap mmp) {
        mmp.put("years",importantMatterService.getYears());
        mmp.put("months",importantMatterService.getMonths());
        return prefix + "/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map<String, Object> map) {
        return getDataTable(deviationAnalysisService.selectDeviationAnalysisList(map));
    }

    @GetMapping("/add/{ids}")
    public String add(@PathVariable("ids") String ids, ModelMap mmp) {
        String[] arr = ids.split(",");
        mmp.put("company", importantMatterService.getCompany(arr[0]));
        mmp.put("project", importantMatterService.getProject(arr[1]));
        mmp.put("build", importantMatterService.getBuild(arr[1]));
        mmp.put("years",importantMatterService.getYears());
        mmp.put("months",importantMatterService.getMonths());
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map<String, Object> map) {
        return toAjax(deviationAnalysisService.addDeviationAnalysis(map));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmp) {
        Map<String,Object> map=deviationAnalysisService.selectDeviationAnalysis(id);
        mmp.put("deviationAnalysis",map);
        mmp.put("build", importantMatterService.getBuild(map.get("proId").toString()));
        mmp.put("years",importantMatterService.getYears());
        mmp.put("months",importantMatterService.getMonths());
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map<String, Object> map) {
        return toAjax(deviationAnalysisService.editDeviationAnalysis(map));
    }

    @PostMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id) {
        return toAjax(deviationAnalysisService.deleteDeviationAnalysis(id));
    }
}
